## This program generates Figure 3 from "Financial Constraints, Sectoral Heterogeneity,
##  and the Cyclicality of Investment" by Cooper Howes

##### Step 0: Preliminaries #####

## Clear variables and data
rm(list=ls())

## Load packages
library(zoo) 

## Load QFR data
qfrdata <- read.csv("QFR data cleaning/qfr_cleaned_data.csv") 

## Pull recession data and create vectors of start/end dates
macrodata <- read.csv("Aggregate_data.csv")
macrodata <- ts(macrodata,start=c(1966,1),end=c(2021,4),frequency=4)

## Create date ranges to be used for recession shading
recessions <- macrodata[,"recession"]
recession_start <- c(0,which(diff(recessions)==1))
recession_end <- which(diff(recessions)==-1)
daterange <- seq(as.Date("1966/3/1"), as.Date("2021/12/1"), by="quarter")
yq <- as.yearqtr(daterange, format = "%Y-%m-%d")
rec_left <- yq[recession_start]
rec_right <- yq[recession_end]

## Plotting dates starting in 1970
daterange2 <- seq(as.Date("1970/3/1"), as.Date("2021/12/1"), by="quarter")
yq2 <- as.yearqtr(daterange2, format = "%Y-%m-%d")

## Extract ratios to be plotted
plotratios <- qfrdata[,c("cur_liab_ratio_t_d","cur_liab_ratio_t_n","cashflow_ratio_t_d","cashflow_ratio_t_n","div_ratio_t_d","div_ratio_t_n")]

## Create moving average for plotting these ratios (default: 4 quarters)
timesmooth <- 4

## Run smoother
smoothratios <- rollmean(plotratios,timesmooth,align="right")

## Convert back to time series
## You will need to change the start date if you change the smoothing parameter
ratios_ts <- window(ts(smoothratios,start=c(1966,4),end=c(2021,4),frequency=4),start=c(1970,1))

## Set parameters for plot windows
w       <- 10         ## Window width
h       <- 6          ## Window height
numrows <- 2          ## Number of rows in each window


############# Loop starts here ###################

## Set names and colors for loops over types
plottypes  <- c("_d","_n")
plotcolors <- c("blue","red")
typenames  <- c("Durable","Nondurable")



##### Plot ratios calculated from QFR on separate lines #####

ratioplotnames <- c("cur_liab_ratio","cashflow_ratio","div_ratio")
longratioplotnames <- c("Current (<1YR) liability share","Net income/capital stock","Dividend rate")
numratioplots <- length(ratioplotnames)

windows(width=10,height=3.5)
m <- matrix(1:numratioplots,nrow = 1,byrow=TRUE)
layout(mat = m)

for(p in 1:numratioplots){

  ## Extract ratios
  var1 <- ratios_ts[,paste0(ratioplotnames[p],"_t_d")]
  var2 <- ratios_ts[,paste0(ratioplotnames[p],"_t_n")]
  
  ## Set y-axis range
  y_range<-range(var1,var2)
  
  ## Set date range
  plot(x=yq2,y=var1,col="blue",type="l",lwd=3,ylim=y_range,ann=FALSE,axes=FALSE,frame=TRUE,pch=1,cex.axis=1.5,
          panel.first=rect(xleft=rec_left,ybottom=-1,xright=rec_right,ytop=2,col="gray69",border=NA))
  axis(1,cex.axis=1.25)
  axis(2,cex.axis=1.25)
  lines(x=yq2,y=var2,lty=2,col="red",lwd=3)
  abline(a=0,b=0,col="black",lwd=1)
  title(main=longratioplotnames[p], col.main="blue",cex.main=1.25,font.main=2,line=0.25)
  
}

## Add legend
par(fig = c(0, 1, 0, 1), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0), new = TRUE)
plot(0, 0, type = "n", bty = "n", xaxt = "n", yaxt = "n")
legend(x="bottom",inset=0,legend=typenames,
     lwd=c(2,2),lty=c(1,2),col=c("blue","red"),cex=1.25,horiz=TRUE)




